package org.ponly.cta4j.drawing.text.glyphsvisitor;

import org.ponly.cta4j.drawing.text.Glyphs;

import java.awt.geom.Rectangle2D;
import java.security.SecureRandom;
import java.util.Random;

/**
 * @author mag
 * @Date 6 mars 2008
 */
public class TranslateAllToRandomPointVisitor implements GlyphsVisitor {
    private Random myRandom = new SecureRandom();
    private double horizontalMargins = 0;
    private double verticalMargins = 0;


    public TranslateAllToRandomPointVisitor() {
    }

    /**
     * @param horizontalmargins the horizontal margin, default 0
     * @param verticalmargins   the vertical margin, default 0
     */
    public TranslateAllToRandomPointVisitor(double horizontalmargins, double verticalmargins) {
        this.horizontalMargins = horizontalmargins;
        this.verticalMargins = verticalmargins;
    }

    public void visit(Glyphs glyphs, Rectangle2D backgroundBounds) {
        double xRange = backgroundBounds.getWidth() - glyphs.getBoundsWidth() - horizontalMargins;
        double yRange = backgroundBounds.getHeight() - glyphs.getBoundsHeight() - verticalMargins;
        double tx = xRange * myRandom.nextDouble() - glyphs.getBoundsX() + horizontalMargins / 2;
        double ty = yRange * myRandom.nextDouble() - glyphs.getBoundsY() + verticalMargins / 2;
        glyphs.translate(tx, ty);
    }
}